For numerical variables, MATLAB provides multiple data types. By default, MATLAB specifies data as double type, representing 64-bit double-precision floating-point. MATLAB operates on double type data using double-precision calculations. For most numerical computing tasks, the double type is recommended.
In addition to the double type, MATLAB provides several other data types that require less storage space than double. These include:
single type — Single-precision floating-point value, 32-bit;
int8 and uint8 types — Signed and unsigned integer types, 8-bit;
int16 and uint16 types — Signed and unsigned integer types, 16-bit;
int32 and uint32 types — Signed and unsigned integer types, 32-bit.
If you need to reduce memory consumption, using the data types introduced above is very useful. For example, it is useful when operating on very large data sets such as images.
Integer Data Types
Integer data types in MATLAB can define integer values within a certain range. For example, int8 type data can represent any 8-bit unsigned integer from -128 to 127. This data type is useful when saving data that can only be stored as integers (such as image data). Table 2-3 lists the value ranges for various integer data types.
Table 2-3: Integer Data Types and Their Value Ranges in MATLAB
| Data Type | Description | Value Range |
|---|---|---|
| int8 | Signed 8-bit integer | -128 ~ 127 |
| uint8 | Unsigned 8-bit integer | 0 ~ 255 |
| int16 | Signed 16-bit integer | -2¹⁵ ~ 2¹⁵-1 |
| uint16 | Unsigned 16-bit integer | 0 ~ 2¹⁶-1 |
| int32 | Signed 32-bit integer | -2³¹ ~ 2³¹-1 |
| uint32 | Unsigned 32-bit integer | 0 ~ 2³²-1 |
Specify values or variables as integer types.
[Example 2-5] Enter in the Command Window:
x=int8(5)
Sets the value of x to 5, with the data type int8. You can use the class function to confirm the data type of a variable. For example:
class(x)
ans=
int8
When converting a numeric value to an integer data type, MATLAB rounds the result to the nearest integer value. For example:
int8(2.7)
ans=
3
For the case where the decimal part is 5, if the number is positive, it rounds up; otherwise, it rounds down. For example:
int8(2.5)
ans=
3
int8(-2.5)
ans=
-3
When converting data larger than the maximum value of an integer data type to that integer type, MATLAB returns the maximum value. For example:
int8(300)
ans=
127
Similarly, during conversion, if the data value is smaller than the minimum value of the data type, the minimum value is returned, which is usually called saturation overflow.
Adding two integer data gives a result that is still an integer. For example:
x=int16(5)+int16(9)
x=
14
class(x)
ans=
int16
Single-Precision Floating-Point Data Type
The single-precision floating-point data type is the single type.
[Example 2-6] Use the single command to specify a numeric value or variable as single type. Enter in the Command Window:
a=single(5)
Sets the value of a to 5 and the data type to single.
When saving data as single type, the required memory is only half of that when saving as double type.
You can use the whos command to compare, for example:
b=5;
whos
Name Size Bytes Class
a 1x1 4 single
b 1x1 8 double
When converting double type data to single type, MATLAB rounds the data to the nearest single-precision floating-point value. This causes slight changes to the data. For example:
format long
single(3.14)
ans=
3.1400001
The function eps returns the precision of a floating-point number. The so-called precision refers to the difference between that floating-point number and the next floating-point number. For example:
format long
eps(5)
ans=
8.881784197001252e-16
The value of eps(x) is related to the size of x; the larger the value of x, the larger the value of eps(x). For example:
eps(50)
ans=
7.105427357601002e-15
The following statement returns the precision of the single-precision floating-point number 5.
x=single(5)
eps(x)
ans=
4.7683716e-07
Note: This result is larger than the precision of the double-precision floating-point number 5. This shows that between two adjacent data points, there are more double-precision floating-point numbers than single-precision ones, meaning the precision of double-precision floating-point numbers is higher than that of single-precision floating-point numbers.